﻿Imports Microsoft.VisualBasic
Imports System.Net

Public Class AttachClass

    Shared Function DeleteSP(ByVal bid As Decimal) As String
        Dim sharePointListPath As String = ConfigurationManager.AppSettings("SharePointServer")

        Dim atadap As New AttachmentTableAdapters.AttachmentsTableAdapter
        Dim atdt As New Attachment.AttachmentsDataTable
        atdt = atadap.GetDataByRefID(bid)

        For Each item As Attachment.AttachmentsRow In atdt

            Dim filename As String = item.FileName
            Dim countfile As Integer = 0
            countfile = atadap.CountExistFile(filename.Trim)
            If filename.Trim <> "" And countfile <= 1 Then
                Dim resp As WebResponse = Nothing
                Dim request As WebRequest = WebRequest.Create(sharePointListPath & filename.Trim)
                request.Credentials = CredentialCache.DefaultCredentials
                request.Method = "DELETE"
                resp = request.GetResponse()
            End If
            
        Next
    End Function
End Class
